home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_04 / 1n04040a < prev    next >
Text File  |  1990-07-23  |  677b  |  52 lines

  1.  
  2. Listing 4
  3.  
  4. const
  5.     HT = chr(9);
  6.     CR = chr(13);
  7.     SPACES = [' ', HT, CR];
  8.  
  9. function atoi(s : string) : integer;
  10.     var
  11.         c : char;
  12.         i, n : integer;
  13.         neg : boolean;
  14.  
  15.     function next : char;
  16.         begin
  17.         if i < length(s) then
  18.             begin
  19.             inc(i);
  20.             next := s[i];
  21.             end
  22.         else
  23.             next := chr(0);
  24.         end;
  25.  
  26.     begin
  27.     i := 0;
  28.     c := next;
  29.     while c in SPACES do
  30.         c := next;
  31.     neg := FALSE;
  32.     if c = '+' then
  33.         c := next
  34.     else if c = '-' then
  35.         begin
  36.         neg := TRUE;
  37.         c := next;
  38.         end;
  39.     n := 0;
  40.     while c in ['0' .. '9'] do
  41.         begin
  42.         n := 10 * n + ord(s[i]) - ord('0');
  43.         c := next;
  44.         end;
  45.     if neg then
  46.         n := -n;
  47.     atoi := n;
  48.     end;
  49.  
  50. ----------
  51.  
  52.